﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using HtmlAgilityPack;


namespace sodexo
{
    public partial class PageResult : PhoneApplicationPage
    {
        private sodexoWebClient sWclient;
        private int currentPageNumber;
        private bool updataing = false;
        public sodexoWebClient SWclient
        {
            get
            {
                return sWclient;
            }
            set
            {
                sWclient = value;
            }
        }
        public PageResult()
        {
            InitializeComponent();
            currentPageNumber = 0;
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            SWclient.SerchPageLoadedEvent += getSearchResults;
            sWclient.getSerchWeb(sWclient.CardType[sWclient.cardTypeIndex].value, sWclient.Districts[sWclient.districtIndex].Value, currentPageNumber);

            List<ScrollBar> scrollBarList = GetVisualChildCollection<ScrollBar>(this);
            foreach (ScrollBar scrollBar in scrollBarList)
            {
                if (scrollBar.Orientation == System.Windows.Controls.Orientation.Horizontal)
                {

                }
                else
                {
                    scrollBar.ValueChanged += new RoutedPropertyChangedEventHandler<double>(verticalScrollBar_ValueChanged);
                }
            }
            this.listBoxResult.ItemsSource = sWclient.Affiliates;
 
        }
        private void verticalScrollBar_ValueChanged(object sender, RoutedEventArgs e)
        {
            if (updataing)
                return;
            ScrollBar scrollBar = (ScrollBar)sender;
            object valueObj = scrollBar.GetValue(ScrollBar.ValueProperty); 
            object maxObj = scrollBar.GetValue(ScrollBar.MaximumProperty); 
            if (valueObj != null && maxObj != null)  
            {             
                double value = (double)valueObj;
                double max = (double)maxObj - 5.0;              
                if (value >= max)              
                {               
                    //读取下一页的数据
                    currentPageNumber++;
                    updataing = true;
                    if (currentPageNumber <= sWclient.PageCount)
                    {
                        performanceProgressBarUpdate.IsIndeterminate = true;
                        sWclient.getSerchWeb(sWclient.CardType[sWclient.cardTypeIndex].value, sWclient.Districts[sWclient.districtIndex].Value, currentPageNumber);
                    }
                }         
            }
        }
        public static List<T> GetVisualChildCollection<T>(object parent) where T : UIElement
        {    List<T> visualCollection = new List<T>();
            GetVisualChildCollection(parent as DependencyObject, visualCollection);
            return visualCollection;
        }
        private static void GetVisualChildCollection<T>(DependencyObject parent, List<T> visualCollection) where T : UIElement
        {    int count = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < count; i++)    
            {        DependencyObject child = VisualTreeHelper.GetChild(parent, i);
                if (child is T)        
                {            visualCollection.Add(child as T);        }        
                else if (child != null)        
                {            GetVisualChildCollection(child, visualCollection);        }
            }
        }
        private void getSearchResults(object sender, HtmlDocumentLoadCompleted args)
        {
            performanceProgressBarUpdate.IsIndeterminate = false;
           // if (args.Error == null)
            {
                int reslutsCount = sWclient.Affiliates.Count;
                if ((reslutsCount > 0))
                {
                    this.PageTitle.Text = "找到商户数目:" + reslutsCount.ToString();
                }
                else
                {
                    this.PageTitle.Text = "找不到商户";
                }
            }
            if (updataing)
                updataing = false;
            
          
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            MenuItem menu = sender as MenuItem;
            ListBoxItem ListItem = this.listBoxResult.ItemContainerGenerator.ContainerFromItem(menu.DataContext) as ListBoxItem;
            sodexoAffiliate selectAffiliate = ListItem.Content as sodexoAffiliate;
            if (selectAffiliate.PhoneNumber != "")
            {
                PhoneCallTask phoneCall = new PhoneCallTask();
                phoneCall.DisplayName = selectAffiliate.Name;
                phoneCall.PhoneNumber = selectAffiliate.PhoneNumber;
                phoneCall.Show();
            }
            
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (sWclient.Affiliates.Count > 0)
            {
                sWclient.Affiliates.Clear();
            }
        }

        private void MenuItem_Click_Copy(object sender, RoutedEventArgs e)
        {
            MenuItem menu = sender as MenuItem;
            ListBoxItem ListItem = this.listBoxResult.ItemContainerGenerator.ContainerFromItem(menu.DataContext) as ListBoxItem;
            sodexoAffiliate selectAffiliate = ListItem.Content as sodexoAffiliate;
            if (selectAffiliate.Address != "")
            {
                Clipboard.SetText(selectAffiliate.Address);
            }
        }
    }
}